package com.ztgf.shopping.service.epg;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.commodity.models.service.ICommodityInfoService;
import com.ztgf.commodity.models.vo.CommodityPriceVO;
import com.ztgf.shopping.models.bo.epg.EpgShopSectionCommodityRelationBO;
import com.ztgf.shopping.models.constant.ShoppingConstant;
import com.ztgf.shopping.models.enums.ShoppingCodeEnum;
import com.ztgf.shopping.models.vo.epg.EpgShopSectionCommodityRelationVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;

import com.ztgf.shopping.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.shopping.mapper.epg.EpgShopSectionCommodityRelationMapper;
import com.ztgf.shopping.models.service.epg.IEpgShopSectionCommodityRelationService;
import com.ztgf.shopping.models.bo.epg.EpgShopSectionCommodityRelationSaveBO;
import com.ztgf.shopping.models.dto.epg.EpgShopSectionCommodityRelationDTO;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 店铺频道栏目商品表控制器
 *
 * @author YUE
 * @Date 2020-05-28 18:40:13
 */
@DubboService
public class EpgShopSectionCommodityRelationServiceImpl extends ServiceImpl<EpgShopSectionCommodityRelationMapper, EpgShopSectionCommodityRelationDTO> implements IEpgShopSectionCommodityRelationService{

    @Resource
    private YmlConfig ymlConfig;

    @Autowired
    private EpgShopSectionCommodityRelationMapper epgShopSectionCommodityRelationMapper;

    @DubboReference
    private ICommodityInfoService iCommodityInfoService;

    @Override
    public boolean add(EpgShopSectionCommodityRelationSaveBO saveBO) {
        EpgShopSectionCommodityRelationDTO dto = new EpgShopSectionCommodityRelationDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    /**
     * 根据商品id删除
     * @param sectionId
     * @return
     */
    public boolean deleteByCommodityId(Long sectionId,Long commodityId){
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("section_id", sectionId);
        wrapper.eq("commodity_id", commodityId);
        return  delete(wrapper);
    }

    /**
     * 根据频道id删除对应的商品
     * @param sectionId
     * @return
     */
    public boolean deleteBySectionId(Long sectionId){
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("section_id", sectionId);
        return delete(wrapper);
    }

    /**
     * 添加必要数据
     * @param epgShopSectionCommodityRelationBO
     * @return
     */
    private EpgShopSectionCommodityRelationBO addMustData(EpgShopSectionCommodityRelationBO epgShopSectionCommodityRelationBO){
        epgShopSectionCommodityRelationBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
        epgShopSectionCommodityRelationBO.setSort(ShoppingCodeEnum.SORT_ASC.getCode());
        return epgShopSectionCommodityRelationBO;
    }

    /**
     * 根据必要查询
     * @param epgShopSectionCommodityRelationBO
     * @return
     */
    private Wrapper<EpgShopSectionCommodityRelationDTO> getBySelectCommodityRelationDTO(EpgShopSectionCommodityRelationBO epgShopSectionCommodityRelationBO){
        Wrapper<EpgShopSectionCommodityRelationDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("is_publish", epgShopSectionCommodityRelationBO.getIsPublish());
        wrapper.eq("section_id", epgShopSectionCommodityRelationBO.getSectionId());
        if(ShoppingCodeEnum.SORT_DESC.getCode() == epgShopSectionCommodityRelationBO.getSort())
            wrapper.orderBy("sort", false);
        else
            wrapper.orderBy("sort", true);
        return wrapper;
    }

    /**
     * 根据条件选择查询 频道栏目商品
     *
     * @param sectionId
     * @return
     */
    @Override
    public List<EpgShopSectionCommodityRelationVO> getListBySectionId(Long sectionId){
        EpgShopSectionCommodityRelationBO epgShopSectionCommodityRelationBO = new EpgShopSectionCommodityRelationBO();
        epgShopSectionCommodityRelationBO.setSectionId(sectionId);
        epgShopSectionCommodityRelationBO = addMustData(epgShopSectionCommodityRelationBO);
        Wrapper<EpgShopSectionCommodityRelationDTO> wrapper = getBySelectCommodityRelationDTO(epgShopSectionCommodityRelationBO);
        List<EpgShopSectionCommodityRelationDTO> epgShopSectionCommodityRelationDTOList = epgShopSectionCommodityRelationMapper.selectPage(new Page<>(0, ShoppingConstant.PLATFORM_SECTION_COMMODITY), wrapper);
        List<EpgShopSectionCommodityRelationVO> epgShopSectionCommodityRelationVOList = new ArrayList<>();
        for (EpgShopSectionCommodityRelationDTO epgSectionCommodityRelationDTO : epgShopSectionCommodityRelationDTOList) {
            EpgShopSectionCommodityRelationVO epgShopSectionCommodityRelationVO = new EpgShopSectionCommodityRelationVO();
            BeanUtils.copyProperties(epgSectionCommodityRelationDTO,epgShopSectionCommodityRelationVO);
            CommodityPriceVO commodityPriceVO = iCommodityInfoService.getByIdOtherInterface(epgSectionCommodityRelationDTO.getCommodityId());//商品
            epgShopSectionCommodityRelationVO.setCommodityPriceVO(commodityPriceVO);
            epgShopSectionCommodityRelationVOList.add(epgShopSectionCommodityRelationVO);
        }
        return epgShopSectionCommodityRelationVOList;
    }







    /**
     * 根据条件 查询 店铺频道栏目商品表
     * @param epgShopSectionCommodityRelationBO
     * @return
     */
    public List<EpgShopSectionCommodityRelationVO> getBySelectShopSectionCommodityRelationInfo(EpgShopSectionCommodityRelationBO epgShopSectionCommodityRelationBO){
        List<EpgShopSectionCommodityRelationVO> epgShopSectionCommodityRelationVOList = new ArrayList<>();
        Wrapper<EpgShopSectionCommodityRelationDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("is_publish", epgShopSectionCommodityRelationBO.getIsPublish());
        wrapper.eq("section_id", epgShopSectionCommodityRelationBO.getSectionId());
        if(ShoppingCodeEnum.SORT_DESC.getCode() == epgShopSectionCommodityRelationBO.getSort())
            wrapper.orderBy("sort", false);
        else
            wrapper.orderBy("sort", true);

        List<EpgShopSectionCommodityRelationDTO> epgShopSectionCommodityRelationDTOList = selectList(wrapper);
        for (EpgShopSectionCommodityRelationDTO sectionCommodityRelationDTOTemp : epgShopSectionCommodityRelationDTOList) {
            CommodityPriceVO commodityPriceVO = iCommodityInfoService.getByIdOtherInterface(sectionCommodityRelationDTOTemp.getCommodityId());
            EpgShopSectionCommodityRelationVO epgShopSectionCommodityRelationVO = new EpgShopSectionCommodityRelationVO();
            BeanUtils.copyProperties(sectionCommodityRelationDTOTemp,epgShopSectionCommodityRelationVO);
            epgShopSectionCommodityRelationVO.setCommodityPriceVO(commodityPriceVO);
            epgShopSectionCommodityRelationVOList.add(epgShopSectionCommodityRelationVO);
        }
        return epgShopSectionCommodityRelationVOList;
    }
}
